<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */

global $logid;
//,$uid,$user;

$pageVars = array();
$errmsg = GetMessageStack();
$userDao = GetUserDAO();
$cityDAO = GetCityDAO();

//can not use function here as it is the subpage,
//global key word does not work, can not get global vars, shit
//use BLOCK_ENTER and BLOCK_END instead
//
//function ProcessBasicInfo() {
//	global $errmsg, $userDao;
//
//	$params = array();
//	$params['nick'] = trim($_POST['nick']);
//	$params['province'] = trim($_POST['province']);
//	$params['city'] = trim($_POST['city']);
//	$params['introduction'] = trim($_POST['introduction']);
//
//	if( !$userDao->UpdateUserBasicInfo( $params ) ){
//		$errmsg->add( 'basic_info', '写入数据库失败' );
//	}
//}

//write to database
if(isset($_POST['action'])) {
	$action=trim($_POST['action']);
	if($action=='basic_info') {
		//use loop to ensure single entrance and single exit
		do{
			$params = array();
			$params['id'] = $logid;
			//$params['nick'] = trim($_POST['nick']);
			$params['province'] = trim($_POST['province']);
			$params['city'] = trim($_POST['city']);
			if(!edp_is_empty_string($params['province'])&&edp_is_empty_string($params['city']))
			{
				$errmsg->add( 'basic_info', '现居地信息不正确', 'error' );
				break;
			}
			$params['location'] = $cityDAO->GetCityID( trim($_POST['city']) );
			$params['introduction'] = edp_db_input(trim($_POST['introduction']));
			if(EdpStrLen($params['introduction'])>300)
			{
				$errmsg->add( 'basic_info', '用户介绍最大长度不超过300字符', 'error' );
				break;
			}
			//$params['logo_path'] = $_POST['logo_path'];

			/*
			//first check params
			if( edp_is_empty_string($params['id'] ) )
				$errmsg->add( 'basic_info', '用户不合法！', 'error' );
			if( edp_is_empty_string($params['province']) )
				$errmsg->add( 'basic_info', '省份不合法！', 'error' );
			if( edp_is_empty_string($params['city']) )
				$errmsg->add( 'basic_info', '城市不合法！', 'error' );
			*/
			
			$pageVars = array_merge( $pageVars, $params );
			if( IsParaOK() == false )     //  $errmsg->size('basic_info') > 0 )
			{
				break;
			}

			if( !$userDao->UpdateUserBasicInfo( $params ) ) {
				$errmsg->add( 'basic_info', '写入数据库失败', 'error' );
			}
			else{
				$errmsg->add( 'basic_info', '更新成功', 'success' );
			}
			
			break;
		}while(1);
	}
	else{
		$errmsg->add( 'basic_info', '未知操作', 'error' );
	}
}
//read from database
//else{
	
//	$userInfo = getUserFromCookie();
//	if( $userInfo ){
		//get user basic_info from database
		$userBasicInfo = $userDao->GetUserBasicInfo( $logid );
		if( $userBasicInfo ){
			$pageVars['id'] = $userBasicInfo['id'];
			$pageVars['nick'] = $userBasicInfo['nick'];
			$pageVars['logo_path'] = $userBasicInfo['logo_path'];
			$pageVars['city'] = $cityDAO->GetCityNameSimple($userBasicInfo['location']);
//			$pageVars['province'] = $cityDAO->GetCityName( $cityDAO->GetProvinceIDByCityID($userBasicInfo['location']) );
			$provinceid = $cityDAO->GetProvinceIDByCityID($userBasicInfo['location']);
			if( $provinceid != 0)
				$pageVars['province'] = $cityDAO->GetCityNameSimple( $provinceid );
			else
				$pageVars['province'] = $pageVars['city'];
				
			$pageVars['introduction'] = $userBasicInfo['introduction'];
		}
		else{
			$errmsg->add( 'basic_info', '读取用户信息失败', 'error' );
		}
//	}
//	else{
//		$errmsg->add( 'basic_info', '登录用户信息不合法', 'error' );
//	}
//}
?>
